package com.csun.nusing.familypresenter;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.FamilyOlderListDateJsonBean;
import com.csun.nusing.bean.jsonbean.FamilyOlderListDateJsonBean.FamilyOlderListDataBean;
import com.csun.nusing.bean.jsonbean.OldrestStateJsonBean;
import com.csun.nusing.presenter.BasePresenter;
import com.csun.nusing.presenter.inter.IHome;
import com.csun.nusing.ui.myview.TabLayout;
import com.csun.nusing.ui.myview.TabLayout.OnSelectedCallBack;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在家属主界面的第一个主页fragment的presenter层 处理里面的业务逻辑 ClassName:
 * FragmentNursingHomePresenter
 *
 * @author fzq
 * @Description: TODO
 * @date 2017-10-20
 */
public class FragmentFamilyHomePresenter extends BasePresenter<IHome> {

    private Context mcontext;
    private String authorization;
    private String userid;
    private TextView bloodoxyTv;
    private TextView bloodpressTv;
    private TextView bloodsugerTv;
    private TextView heartrateTv;
    private TextView huxiTv;
    private TextView temperatureTv;
    private ImageView photoimageView;
    private TextView nameTv;
    private TabLayout tablay;
    private int currentposition;
    protected FamilyOlderListDateJsonBean familyoldrestlistbean;
    protected OldrestStateJsonBean oldreststatebean;
    protected FamilyOlderListDataBean dataBean;
    private String roomCode;

    public FragmentFamilyHomePresenter(IHome view) {
        super(view);
    }

    public void getOlderList(Context context, TabLayout horlv,
                             TextView bloodoxyTv, TextView bloodpressTv, TextView bloodsugerTv,
                             TextView heartrateTv, TextView huxiTv, TextView temperatureTv,
                             ImageView photoimageView, TextView nameTv) {
        this.mcontext = context;
        this.tablay = horlv;
        this.bloodoxyTv = bloodoxyTv;
        this.bloodpressTv = bloodpressTv;
        this.bloodsugerTv = bloodsugerTv;
        this.heartrateTv = heartrateTv;
        this.huxiTv = huxiTv;
        this.temperatureTv = temperatureTv;
        this.photoimageView = photoimageView;
        this.nameTv = nameTv;

        /**
         * 1获取看护人列表 2.获取健康数据 3.将健康数据显示
         */
        authorization = (String) SPUtils.get(context, "authorization", "");
        userid = (String) SPUtils.get(context, "userId", "");
        Map<String, String> param = new HashMap<String, String>();
        OkHttpTask.newInstance(context).post(param, OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            Log.e("fzq", "-------" + result);
                            Gson gson = new Gson();
                            familyoldrestlistbean = gson.fromJson(result,
                                    FamilyOlderListDateJsonBean.class);
                            if (familyoldrestlistbean != null) {
                                if (familyoldrestlistbean.getData() != null) {
                                    getHealthList(familyoldrestlistbean
                                            .getData());
                                    controlList();
                                }
                            }
                        }
                    }
                }).start(UrlBean.FAMILYOLDERLISTURL);
    }

    /**
     * 获取健康报告
     *
     * @param @param nursingoldrestlistbean2
     * @return void
     * @throws
     * @Description: TODO
     * @author fzq
     * @date 2017-10-30
     */
    protected void getHealthList(final List<FamilyOlderListDataBean> list) {
        // 加载第一个
        if (list.size() > 0) {
            initData(list, 0);
        }

    }

    /**
     * 加载数据
     *
     * @param @param list
     * @param @param i
     * @return void
     * @throws
     * @Description: TODO
     * @author fzq
     * @date 2017-10-30
     */
    private void initData(List<FamilyOlderListDataBean> list, int i) {
        currentposition = i;
        Log.e("fzq", "-----home  initData");
        if (i < list.size()) {
            /**
             * 传数据到view
             */
            dataBean = familyoldrestlistbean.getData().get(i);
            roomCode = "-";
            if (dataBean.getBuildingName() == null) {
                roomCode = "-";
            } else if (dataBean.getRoomNo() == null) {
                roomCode = "-";
            } else if (dataBean.getBedNo() == null) {
                roomCode = "-";
            } else if (dataBean.getBuildingName().equals("null")) {
                roomCode = "-";
            } else if (dataBean.getRoomNo().equals("null")) {
                roomCode = "-";
            } else if (dataBean.getBedNo().equals("null")) {
                roomCode = "-";
            } else {
                roomCode = dataBean.getBuildingName()
                        + dataBean.getRoomNo() + mcontext.getString(R.string.room) + dataBean.getBedNo()
                        + mcontext.getString(R.string.bed);

            }

            Glide.with(mcontext)
                    .load(dataBean.getHeadPicture())
                    .into(photoimageView);

            /**
             * 传数据到view
             */
            getViewRef().sendId("" + dataBean.getId(), "" + dataBean.getName(),
                    "" + dataBean.getHeadPicture(),
                    "" + dataBean.getBirthday(), "" + dataBean.getAge(),
                    "" + roomCode, "" + dataBean.getSex(),
                    "" + dataBean.getEgContact(), "" + dataBean.getEgPhone(),
                    "" + dataBean.getRoomId(), "" + dataBean.getBedNo());

            nameTv.setText("" + list.get(i).getName());
            /**
             * 请求
             */
            authorization = (String) SPUtils.get(mcontext, "authorization", "");
            Map<String, String> param = new HashMap<String, String>();
            param.put("id", "" + list.get(i).getId());
            param.put("headpic", "" + list.get(i).getHeadPic());
            OkHttpTask.newInstance(mcontext).post(param, OkType.form)
                    .addHeader("authorization", authorization)
                    .enqueue(new IOkTaskCallback() {
                        @Override
                        public void onSuccess(String result) {
                            if (result != null) {
                                Log.e("fzq", "-----home  " + result);
                                Gson gson = new Gson();
                                oldreststatebean = gson.fromJson(result,
                                        OldrestStateJsonBean.class);
                                if (oldreststatebean != null) {
                                    controlViewpager(oldreststatebean);
                                }
                            }
                        }
                    }).start(UrlBean.OLDRESTINFOURL);
        }
    }

    protected void controlViewpager(OldrestStateJsonBean oldreststatebean2) {
        if (oldreststatebean2.getData() == null) {
            return;
        }
        if (oldreststatebean2.getData().getHeadpicture() != null) {
            Glide.with(mcontext)
                    .load(oldreststatebean2.getData().getHeadpicture())
                    .into(photoimageView);
        }
        bloodoxyTv
                .setText(""
                        + (oldreststatebean2.getData().getBoxvygenStatus() != null ? oldreststatebean2
                        .getData().getBoxvygenStatus() : mcontext.getString(R.string.bed_detail_none)));
        bloodpressTv
                .setText(""
                        + (oldreststatebean2.getData().getBpressureStatus() != null ? oldreststatebean2
                        .getData().getBpressureStatus() : mcontext.getString(R.string.bed_detail_none)));
        bloodsugerTv
                .setText(""
                        + (oldreststatebean2.getData().getBsugarStatus() != null ? oldreststatebean2
                        .getData().getBsugarStatus() : mcontext.getString(R.string.bed_detail_none)));
        heartrateTv
                .setText(""
                        + (oldreststatebean2.getData().getHeartrateStatus() != null ? oldreststatebean2
                        .getData().getHeartrateStatus() : mcontext.getString(R.string.bed_detail_none)));
        huxiTv.setText(""
                + (oldreststatebean2.getData().getBreatheStatus() != null ? oldreststatebean2
                .getData().getBreatheStatus() : mcontext.getString(R.string.bed_detail_none)));
        temperatureTv
                .setText(""
                        + (oldreststatebean2.getData().getTemperatureStatus() != null ? oldreststatebean2
                        .getData().getTemperatureStatus() : mcontext.getString(R.string.bed_detail_none)));
        nameTv.setText(""
                + (oldreststatebean2.getData().getOldName() != null ? oldreststatebean2
                .getData().getOldName() : mcontext.getString(R.string.bed_detail_none)));

    }

    /**
     * 控制viewpager和tab
     *
     * @param
     * @return void
     * @throws
     * @Description: TODO
     * @author fzq
     * @date 2017-10-30
     */
    private void controlList() {
        for (int i = 0; i < familyoldrestlistbean.getData().size(); i++) {
            tablay.addTab(i, familyoldrestlistbean.getData().get(i).getName());

        }
        tablay.initStrip();
        tablay.setOnSelectedCallBack(new OnSelectedCallBack() {
            @Override
            public void selected(int position) {
                initData(familyoldrestlistbean.getData(), position);
                dataBean = familyoldrestlistbean.getData().get(position);
                roomCode = "-";
                if (dataBean.getBuildingName() == null) {
                    roomCode = "-";
                } else if (dataBean.getRoomNo() == null) {
                    roomCode = "-";
                } else if (dataBean.getBedNo() == null) {
                    roomCode = "-";
                } else if (dataBean.getBuildingName().equals("null")) {
                    roomCode = "-";
                } else if (dataBean.getRoomNo().equals("null")) {
                    roomCode = "-";
                } else if (dataBean.getBedNo().equals("null")) {
                    roomCode = "-";
                } else {
                    roomCode = dataBean.getBuildingName()
                            + dataBean.getRoomNo() + mcontext.getString(R.string.room) + dataBean.getBedNo()
                            + mcontext.getString(R.string.bed);
                }
                /**
                 * 传数据到view
                 */
                getViewRef().sendId("" + dataBean.getId(),
                        "" + dataBean.getName(),
                        "" + dataBean.getHeadPicture(),
                        "" + dataBean.getBirthday(), "" + dataBean.getAge(),
                        "" + roomCode, "" + dataBean.getSex(),
                        "" + dataBean.getEgContact(),
                        "" + dataBean.getEgPhone(), "" + dataBean.getRoomId(), "" + dataBean.getBedNo());
            }
        });

    }
}
